﻿using System;

namespace SqueezeboxLib
{
    /// <summary>
    /// Represents a <b>SqueezeboxCommandResult</b>
    /// </summary>
    class SqueezeboxCommandResult : SqueezeboxCommand
    {
        /// <summary>
        /// Gets the squeezebox command results, if any
        /// </summary>
        public string[] Results { get; private set; }

        /// <summary>
        /// Creates a new instance of the <b>SqueezeboxCommandResult</b> class.
        /// </summary>
        /// <param name="playerID"></param>
        /// <param name="command"></param>
        /// <param name="results"></param>
        public SqueezeboxCommandResult(string playerID, string command, params string[] results)
            : base(playerID, command)
        {
            Results = results;
        }

        /// <summary>
        /// Parses a CLI formatted command response line into a <b>SqueezeboxCommandResult</b>
        /// </summary>
        /// <param name="line"></param>
        /// <returns></returns>
        public SqueezeboxCommandResult Parse(string line)
        {
            // <player mac> <command> <?> <result 1> ... <result N>
            string[] split = line.Split(' ');

            try
            {
                string playerAddress = split[0];
                string command = split[1];
                string resultString = line.Split(new[] {'?'}, 2)[1];
                string[] results = resultString.Split(' ');

                return new SqueezeboxCommandResult(playerAddress, command, results);
            }
            catch (Exception ex)
            {
                throw new UnknownCommandResultException(ex.Message);
            }
        }
    }
}
